/**
 * Created by lvlq on 16/2/27.
 */
var code = "0835451787531745,http://bc.zhsh.co/?2083545178753174555aad00";
var soap = require('soap');
var Q = require("q");
//var url = 'http://shelltac.yesno.com.cn/CcnOutService.asmx?wsdl';
var url = 'http://accode.zhsh.co/CcnOutService.asmx?wsdl';

var parseString = require('xml2js').parseString;

module.exports = function (ip, acCode) {
    var defer = Q.defer();
    soap.createClient(url, function (err, client) {
        console.log({
            //"userID": "AL83L90L9L29DK9K525A222D211D222D",
            "userID": "PWI092K98LA893KLS9QGZV61JBXN81",
            //"userPwd": "88OKIKJ89K290KDBBZJ19KYRQ0S0O1S1",
            "userPwd": "8OMZVBT61JH29J91JS81KL285IWK",
            "ip": ip,
            "acCode": acCode,
            "language": "zh-cn",
            "channel": "21"
        });

        client.Get_AcCodeInterface({
            //"userID": "AL83L90L9L29DK9K525A222D211D222D",
            "userID": "PWI092K98LA893KLS9QGZV61JBXN81",
            //"userPwd": "88OKIKJ89K290KDBBZJ19KYRQ0S0O1S1",
            "userPwd": "8OMZVBT61JH29J91JS81KL285IWK",
            "ip": ip,
            "acCode": acCode,
            "language": "zh-cn",
            "channel": "21"
        }, function (err, result) {
            if (err) {
                console.log("Error:" + err.message);
                defer.reject(err);
            }
            else {
                console.log(result);
                if (!result.Get_AcCodeInterfaceResult) defer.reject(new Error(result.reply));
                else {
                    parseString(result.reply, function (err, data) {
                        if (err) defer.reject(err);
                        else defer.resolve(data);
                    });
                }
            }
        });
    });

    return defer.promise;
};